package com.muy.redis.utils;

import com.google.common.collect.Sets;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.muy.redis.view.window.redis.bean.MrRedisConnectionParam;
import com.muy.redis.view.window.redis.bean.MrRedisDb;
import com.muy.redis.view.window.redis.editor.KeyValueDisplayVirtualFile;
import com.muy.redis.view.window.redis.tree.MTTreeCellMrRedisDb;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @Author jiyanghuang
 * @Date 2023/9/16 22:45
 */
public class MrRedisConnectionUtils {

    private static Map<String, RedisPoolManager> connectionRedisMap = new HashMap<>();
    private static Map<String, CopyOnWriteArraySet<KeyValueDisplayVirtualFile>> connectionDbEditorMap = new HashMap<>();

    public static List<MrRedisDb> fetchDb(MrRedisConnectionParam connectionInfo) {
        RedisPoolManager redisPoolManager = connectionRedisMap.get(connectionInfo.getId());
        if (null == redisPoolManager) {
            redisPoolManager = new RedisPoolManager(connectionInfo);
            connectionRedisMap.put(connectionInfo.getId(), redisPoolManager);
        }
        List<MrRedisDb> redisDbList = Lists.newArrayList();
        int dbCount = redisPoolManager.getDbCount();
        for (int i = 0; i < dbCount; i++) {
            Long keyCount = redisPoolManager.dbSize(i);
            if (null == keyCount) {
                return redisDbList;
            }
            MrRedisDb mrRedisDb = new MrRedisDb();
            mrRedisDb.setIndex(i);
            mrRedisDb.setKeyCount(keyCount);
            mrRedisDb.setConnectionId(connectionInfo.getId());
            redisDbList.add(mrRedisDb);
        }
        return redisDbList;
    }

    /**
     * @param project
     * @param redisDbs
     * @return
     */
    public static List<MTTreeCellMrRedisDb> convert(Project project, List<MrRedisDb> redisDbs) {
        List<MTTreeCellMrRedisDb> re = Lists.newArrayList();
        for (MrRedisDb redisDb : redisDbs) {
            MTTreeCellMrRedisDb mtTreeCellMrRedisDb = new MTTreeCellMrRedisDb(project, redisDb);
            re.add(mtTreeCellMrRedisDb);
        }
        return re;
    }

    /**
     * @param connectionParam
     * @return
     */
    public static RedisPoolManager findRedisPoolManager(MrRedisConnectionParam connectionParam) {
        RedisPoolManager redisPoolManager = connectionRedisMap.get(connectionParam.getId());
        if (null == redisPoolManager) {
            redisPoolManager = new RedisPoolManager(connectionParam);
            connectionRedisMap.put(connectionParam.getId(), redisPoolManager);
        }
        return redisPoolManager;
    }

    /**
     * @param connectionId
     * @param virtualFile
     */
    public static void addEditorToMap(String connectionId, KeyValueDisplayVirtualFile virtualFile) {
        CopyOnWriteArraySet<KeyValueDisplayVirtualFile> keyValueDisplayVirtualFiles = connectionDbEditorMap.get(connectionId);
        if (null == keyValueDisplayVirtualFiles) {
            keyValueDisplayVirtualFiles = Sets.newCopyOnWriteArraySet();
            keyValueDisplayVirtualFiles.add(virtualFile);
            connectionDbEditorMap.put(connectionId, keyValueDisplayVirtualFiles);
        } else {
            keyValueDisplayVirtualFiles.add(virtualFile);
        }
    }

    /**
     * 关闭connection时关闭所有打开的editor
     *
     * @param connectionId
     * @param project
     */
    public static void closeAllEditor(String connectionId, Project project) {
        CopyOnWriteArraySet<KeyValueDisplayVirtualFile> keyValueDisplayVirtualFiles = connectionDbEditorMap.get(connectionId);
        if (CollectionUtils.isEmpty(keyValueDisplayVirtualFiles)) {
            return;
        }

        Iterator<KeyValueDisplayVirtualFile> iterator = keyValueDisplayVirtualFiles.iterator();
        while (iterator.hasNext()) {
            KeyValueDisplayVirtualFile keyValueDisplayVirtualFile = iterator.next();
            // close editor
            FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
            fileEditorManager.closeFile(keyValueDisplayVirtualFile);
        }

        connectionDbEditorMap.remove(connectionId);
    }

    /**
     * 关闭tab时移除connection-editor
     */
    public static void removeEditor(String connectionId, KeyValueDisplayVirtualFile virtualFile) {
        CopyOnWriteArraySet<KeyValueDisplayVirtualFile> keyValueDisplayVirtualFiles = connectionDbEditorMap.get(connectionId);
        if (org.apache.commons.collections.CollectionUtils.isEmpty(keyValueDisplayVirtualFiles)) {
            return;
        }
        keyValueDisplayVirtualFiles.remove(virtualFile);
    }
}
